import {wxSappLogin} from '../api/user.js'
import xeUtils from 'xe-utils'
export const cardidReply = (card) => {
	var l1 = card.substr(0, 3);
	var l3 = '********';
	var l2 = card.substr(14);
	return l1 + l3 + l2;
}
export const toast = (title, time) => {
	uni.showToast({
		title: title,
		duration: time ? time : 2000
	});
}

export const showModal = (title, content) => {
	uni.showModal({
	    title: title,
	    content: content,
		showCancel:false,
	    success: function (res) {
	        if (res.confirm) {
	            console.log('用户点击确定');
	        } else if (res.cancel) {
	            console.log('用户点击取消');
	        }
	    }
	});
}

export const userLogin = () =>{
	uni.login({
	    success: (res) => {
	        uni.getUserInfo({
	            success: (infoRes) => {
					uni.setStorageSync('userInfo',infoRes)
	                /**
	                 * 实际开发中，获取用户信息后，需要将信息上报至服务端。
	                 * 服务端可以用 userInfo.openId 作为用户的唯一标识新增或绑定用户信息。
	                 */
					wxSappLogin({code:res.code,data:infoRes.encryptedData,iv:infoRes.iv}).then(ret=>{
						console.log(ret,'2')
						if(ret.data.status == 1){
							uni.setStorageSync('juyou_ticket_key',ret.data.result.ticket)
							uni.redirectTo({
							    url: '/pages/index/menus'
							});
						}else{
							uni.navigateTo({
							    url: '/pages/views/users/bing-mobile/bing-mobile?ticket='+ret.data.result
							});
						}
					})
	                console.log(infoRes,res);
	            }
	        });
	    },
	    fail: (err) => {
	        console.error('授权登录失败：' + JSON.stringify(err));
	    }
	});
}

export default {
	cardidReply,
	toast,
	showModal,
	userLogin,
	xeUtils
}
